package com.central.apps.handler.contract;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.apps.handler.until.DateUtiles;
import com.central.bizcommon.model.flow.FlowNode;
import com.central.bizcommon.model.flow.FlowOpinion;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
public class ConpatentHandler extends LvBaseHandler {

    @Autowired
    private HandlerService handlerService;
    @Autowired
    private FileSynUtilService fileSynUtilService;
    @Autowired
    private ConchangeHandler conchangeHandler;
    @Override
    public void afterExcuteFlow(String app, Long id, FlowNode flowNode, FlowOpinion op, String event) throws Exception {

        System.out.println("流程审批后，进入代码==============================flowNode.getId() =========" + flowNode.getId());



        //提交审批后，状态更改成审批中
        if (flowNode.getId().equals("submit")) {
            boolean flag = op.isFlag();
            if (flag) {
                String sql = "select * from  gq_consigns where id = " + id;
                List<Map<String, Object>> list = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(list)) {
                    //conchangeHandler.updateStatus((String) list.get(0).get("concode"), "审批中");
                    conchangeHandler.updateStatusByid(id,"审批中");

                }
            }
        }
        /*
         * 合同签订审批通过后，插入到合同台账数据
         * */
        if (flowNode.getId().equals("approval")) {
            boolean flag = op.isFlag();
            if (flag) {

                String sql = "select * from  gq_conpatent where id = " + id;
                List<Map<String, Object>> list = handlerService.findBySql(sql);
                if (CollectionUtils.isNotEmpty(list)) {
                    for (Map<String, Object> objectMap : list) {
                        //通过插入合同台账
                        //查询收付款节点个数，赋值到台账
                        String surplusnode="0";
                        String sql5 = "select * from gq_connode where concode = '" + objectMap.get("concode") + "' and ispaynode ='是'";
                        List<Map<String, Object>> list5 = handlerService.findBySql(sql5);
                        if (CollectionUtils.isNotEmpty(list5)) {
                            surplusnode=list5.size()+"";
                        }
                        //关联合同个数
                        String relationnum = "0";
                        if(objectMap.get("relationname")!=null&&!"".equals(objectMap.get("relationname"))){
                            relationnum ="1";
                        }
                        Map<String, Object> map = new HashMap<>();
                        //map.put("totalprice", objectMap.get("totalprice"));//合同金额（含税）
                        map.put("nototalprice", objectMap.get("nototalprice"));//合同金额（不含税）
                        map.put("condept", objectMap.get("condept"));//合同责任部门
                        map.put("conuser", objectMap.get("createuserdesc"));//合同经办人
                        map.put("closttime", objectMap.get(""));//关闭时间 合同关闭时，需要走一个流程，走完流程后，赋值合同关闭时间
                        map.put("taketime", objectMap.get("starttime"));//生效时间
                        map.put("conoppcom", objectMap.get("oppcompany"));//合同对方单位
                        //map.put("constatus", objectMap.get("constatus"));//合同状态
                        map.put("conterm", objectMap.get("endtime"));//合同期限
                        map.put("maincon", objectMap.get("maincon"));//关联合同编号
                        map.put("isframecon", objectMap.get("isframecon"));//是否为框架合同
                        map.put("isadcharge", objectMap.get("isadcharge"));//是否预付款
                        map.put("relationprocode", objectMap.get("relationprocode"));//关联项目编号
                        map.put("createuserdesc", objectMap.get("createuserdesc"));//创建人
                        map.put("status", objectMap.get("status"));//审批状态
                        map.put("relationproname", objectMap.get("relationproname"));//关联项目名称
                        map.put("relationname", objectMap.get("relationname"));//关联合同名称
                        map.put("starttime", objectMap.get("starttime"));//合同开始时间
                        map.put("endtime", objectMap.get("endtime"));//合同结束时间
                        map.put("purchasenum", objectMap.get("purchasenum"));//采购预订单
                        map.put("contemplate", objectMap.get("contemplate"));//合同模板
                        map.put("contemplatename", objectMap.get("contemplatename"));//合同模板名称
                        map.put("oppcompany", objectMap.get("oppcompany"));//对方单位名称



                        map.put("concode", objectMap.get("concode"));//合同编号
                        map.put("conname", objectMap.get("conname"));//合同名称
                        map.put("surplusnode", surplusnode);//剩余收付款节点个数
                        map.put("relationnum", relationnum);//关联合同数
                        map.put("contype", "专利合同");//专利合同
                        map.put("constatus", "已签订");//合同状态
                        map.put("title", objectMap.get("title"));//标题
                        map.put("patentname", objectMap.get("patentname"));//关专利名称
                        map.put("inventor", objectMap.get("inventor"));//发明人
                        map.put("patenttype", objectMap.get("patenttype"));//专利类型
                        map.put("patentapplicant", objectMap.get("patentapplicant"));//专利申请人
                        map.put("totalprice", objectMap.get("totalprice"));//预算金额
                        map.put("subjectno", objectMap.get("subjectno"));//课题号
                        //map.put("budget", objectMap.get("endtime"));//
                        map.put("entrustedagency", objectMap.get("entrustedagency"));//是否需要委托代理
                        map.put("agencyname", objectMap.get("agencyname"));//代理机构名称
                        map.put("agencycontacts", objectMap.get("agencycontacts"));//代理机构哦联系人
                        map.put("phone", objectMap.get("phone"));//联系电话
                        map.put("confirminfo", objectMap.get("confirminfo"));//确认信息
                        map.put("isnecessary", objectMap.get("isnecessary"));//专利是否必要
                        map.put("isappropriate", objectMap.get("isappropriate"));//发明人贡献排名是否合适
                        map.put("isbudreasonable", objectMap.get("isbudreasonable"));//预算是否合理
                        map.put("isinnovate", objectMap.get("isinnovate"));//是否创新
                        map.put("issensitive", objectMap.get("issensitive"));
                        map.put("isdeclareinfo", objectMap.get("isdeclareinfo"));
                        map.put("ismoneyguarantee", objectMap.get("ismoneyguarantee"));
                        map.put("isproject", objectMap.get("isproject"));
                        map.put("isksensitive", objectMap.get("isksensitive"));
                        map.put("isagree", objectMap.get("isagree"));
                        map.put("relevantinfo", objectMap.get("relevantinfo"));
                        map.put("tecdisclosure", objectMap.get("tecdisclosure"));
                        map.put("pubevidence", objectMap.get("pubevidence"));
                        handlerService.insert("gq_conaccount", map);

                        //添加到合同信息表中
                        Map<String, Object> infomap = new HashMap<>();
                        infomap.put("concode", objectMap.get("concode"));//合同编号
                        infomap.put("conname", objectMap.get("conname"));//合同名称
                        infomap.put("typecode", objectMap.get("contype"));//合同类型
                        //infomap.put("constatus", objectMap.get("constatus"));//合同状态
                        infomap.put("constatus", "已签订");//合同状态
                        infomap.put("deptcode", objectMap.get("deptcode"));//合同部门
                        infomap.put("performstatus", "未开始");//合同部门
                        infomap.put("relationprocode", objectMap.get("relationprocode"));//项目编号
                        handlerService.insert("gq_coninfo", infomap);
                        //查询合同节点，插入到合同节点变更表
                        String concode = (String) objectMap.get("concode");
                        String sql2 = "select * from gq_connode where concode = '" + concode + "'";
                        List<Map<String, Object>> list2 = handlerService.findBySql(sql2);
                        if (CollectionUtils.isNotEmpty(list2)) {
                            for (Map<String, Object> stringObjectMap : list2) {
                                stringObjectMap.put("nnodecode", stringObjectMap.get("nodecode"));
                                stringObjectMap.put("nnodename", stringObjectMap.get("nodename"));
                                stringObjectMap.put("nstarttime", stringObjectMap.get("starttime"));
                                stringObjectMap.put("nendtime", stringObjectMap.get("endtime"));
                                stringObjectMap.put("nreviewtime", stringObjectMap.get("reviewtime"));
                                stringObjectMap.put("nchestandard", stringObjectMap.get("chestandard"));
                                stringObjectMap.put("nispaynode", stringObjectMap.get("ispaynode"));
                                stringObjectMap.put("nconinfocode", stringObjectMap.get("coninfocode"));
                                stringObjectMap.put("nmilepostid", stringObjectMap.get("milepostid"));
                                stringObjectMap.put("nmainpayment", objectMap.get("mainpayment"));
                                stringObjectMap.put("nreceivingamounttax", objectMap.get("receivingamounttax"));
                                stringObjectMap.put("nreceivingamount", objectMap.get("receivingamount"));
                                handlerService.insert("gq_connodenew", stringObjectMap);
                            }
                        }
                        //将上传的文件插入到合同台账的
                        fileSynUtilService.insertFiles(app, id, concode);
                    }

                    //合同签订完，状态改为已签订
                    conchangeHandler.updateStatusByid(id, "已签订");
                    //流程归档
                    fileSynUtilService.processarc(app,id+"","合同签订",(String) list.get(0).get("concode"),(String) list.get(0).get("conname"));
                }


            }
        }
    }
}
